package com.bltec.emergency.controller.foreignController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ForeignMain;
import com.bltec.emergency.service.foreignService.ForeignMainService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import com.bltec.emergency.utils.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/foreignMain")
@RequiresRoles("superManger")
public class ForeignMainController {
    @Resource
    private ForeignMainService foreignService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllForeignMain")
    public ResponseJson getUser() {
        try {
            List<ForeignMain> foreign = foreignService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(foreign));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

/*    private String base;
    private Long vesselNumber;
    private Long sorties;
    private String rescueApply;
    private String rescueSituation;
    private String rescueReward;*/
    @RequestMapping("addForeignMain")
    public ResponseJson addUser(String base, Long vesselNumber, Long sorties,
                                @RequestParam(value = "rescueApply",required = false) MultipartFile rescueApply ,
                                @RequestParam(value = "rescueSituation",required = false) MultipartFile rescueSituation,
                                @RequestParam(value = "rescueReward",required = false) MultipartFile rescueReward) {
        try {
            String rescueApplyPath= FileUtils.writeToFile(rescueApply.getBytes(), ResourceUtils.getURL("classpath:").getPath(),rescueApply.getOriginalFilename());
            String rescueSituationPath= FileUtils.writeToFile(rescueSituation.getBytes(), ResourceUtils.getURL("classpath:").getPath(),rescueSituation.getOriginalFilename());
            String rescueRewardPath= FileUtils.writeToFile(rescueReward.getBytes(), ResourceUtils.getURL("classpath:").getPath(),rescueReward.getOriginalFilename());
            ForeignMain foreignMain = new ForeignMain();
            foreignMain.setBase(base);
            foreignMain.setVesselNumber(vesselNumber);
            foreignMain.setSorties(sorties);
            foreignMain.setRescueApply(rescueApplyPath);
            foreignMain.setRescueSituation(rescueSituationPath);
            foreignMain.setRescueReward(rescueRewardPath);
            foreignMain.setEnable(true);
            foreignMain.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignMain.setCreateTime(DateUtil.dateNow());
            foreignService.add(foreignMain);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }


    @RequestMapping("updateForeignMain")
    public ResponseJson updateUser(Long id,String base, Long vesselNumber, Long sorties,
                                   @RequestParam(value = "rescueApply",required = false) MultipartFile rescueApply ,
                                   @RequestParam(value = "rescueSituation",required = false) MultipartFile rescueSituation,
                                   @RequestParam(value = "rescueReward",required = false) MultipartFile rescueReward) {
        try {
            String rescueApplyPath= FileUtils.writeToFile(rescueApply.getBytes(), ResourceUtils.getURL("classpath:").getPath(),rescueApply.getOriginalFilename());
            String rescueSituationPath= FileUtils.writeToFile(rescueSituation.getBytes(), ResourceUtils.getURL("classpath:").getPath(),rescueSituation.getOriginalFilename());
            String rescueRewardPath= FileUtils.writeToFile(rescueReward.getBytes(), ResourceUtils.getURL("classpath:").getPath(),rescueReward.getOriginalFilename());
            ForeignMain foreignMain = foreignService.getForeignById(id);
            foreignMain.setBase(base);
            foreignMain.setVesselNumber(vesselNumber);
            foreignMain.setSorties(sorties);
            foreignMain.setRescueApply(rescueApplyPath);
            foreignMain.setRescueSituation(rescueSituationPath);
            foreignMain.setRescueReward(rescueRewardPath);
            foreignMain.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignMain.setUpdateTime(DateUtil.dateNow());
            foreignService.updateForeignById(foreignMain);
            return new ResponseJson(Constant.OK, JSON.toJSON(foreignMain));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteForeignMain")
    public ResponseJson setDisable(Long id) {
        if (null != foreignService.getForeignById(id)) {
            if (foreignService.deleteForeignById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
